package com.atguigu.sso.client.Controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HelloController {



     @Value("${sso.server.url}")
     public String url;



   @ResponseBody
   @GetMapping("/hello")
   public String hello(){
       return "hello";
   }

    /**
     * 判断 单点登录
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/employee")
    public String employee(Model model, HttpSession session, @RequestParam(value = "sso_token",required = false) String ssoToken){


        if(!StringUtils.isEmpty(ssoToken)){
           session.setAttribute("loginUser",ssoToken);
       }
        Object loginUser = session.getAttribute("loginUser");

        if(loginUser==null){
            return "redirect:"+url+"?url=http://client1.com:8081/employee";
        }else{
            List<String> emps = new ArrayList<>();

            emps.add("张三");
            emps.add("李四");

            model.addAttribute("emps", emps);
            return "employees";
        }
    }









}
